package com.htgd.gardenexpo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.htgd.gardenexpo.entity.GeConWeather;
import com.htgd.gardenexpo.dao.GeConWeatherMapper;
import com.htgd.gardenexpo.service.IGeConWeatherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.utils.GaoDeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 天气 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-04-08
 */
@Service
public class GeConWeatherServiceImpl extends ServiceImpl<GeConWeatherMapper, GeConWeather> implements IGeConWeatherService {

    @Autowired
    private GeConWeatherMapper geConWeatherMapper;

    @Autowired
    private GaoDeUtils gaoDeUtils;

    @Override
    public JSONObject getWeather() {
        GeConWeather one = lambdaQuery().eq(GeConWeather::getId, "1").one();

        LocalDateTime now = LocalDateTime.now();
        if (now.isBefore(one.getUpdateTime().plusMinutes(5))) {
            return JSONObject.parseObject(one.getContent());
        }else {
            JSONObject weather = gaoDeUtils.getWeather();
            lambdaUpdate().eq(GeConWeather::getId,"1")
                    .set(GeConWeather::getContent,weather.toString())
                    .set(GeConWeather::getUpdateTime,now)
                    .update();
            return weather;
        }
    }
}
